﻿using System.Collections.Generic;
using System.Linq;
using lindyaka.Models.Administration.Panel;
using lindyaka.Repository.Interfaces;

namespace lindyaka.Services.Administration
{
    public class Panel
    {
        private readonly IRoleRepository _repositoryRoles;

        public Panel()
        {
            _repositoryRoles = new Repository.Entities.Role();
        }

        public Panel(IRoleRepository repository)
        {
            _repositoryRoles = repository;
        }

        public IEnumerable<Role> Get()
        {
            return (from r in _repositoryRoles.Find()
                    select new Role
                        {
                            Name = r.Name,
                            RoleId = r.RoleId
                        }).AsEnumerable();
        }

        public Role Get(int roleId)
        {
            Domain.Entities.Role role = _repositoryRoles.Find(r => r.RoleId == roleId);
            return new Role
                {
                    Name = role.Name,
                    RoleId = role.RoleId
                };
        }

        public void Add(Role role)
        {
            _repositoryRoles.Add(new Domain.Entities.Role
                {
                    Name = role.Name,
                    RoleId = role.RoleId
                });
        }

        public void Update(Role role)
        {
            _repositoryRoles.Update(new Domain.Entities.Role
                {
                    Name = role.Name,
                    RoleId = role.RoleId
                });
        }

        public void Remove(Role role)
        {
            _repositoryRoles.Remove(r => r.RoleId == role.RoleId);
        }
    }
}